package com.ruiaa.meeting.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.ruiaa.meeting.common.CommonResponse;
import com.ruiaa.meeting.common.Constant;
import com.ruiaa.meeting.common.ResponseCode;
import com.ruiaa.meeting.model.Reservation;
import com.ruiaa.meeting.model.User;
import com.ruiaa.meeting.model.dto.ReservationDto;
import com.ruiaa.meeting.model.vo.ReservationVo;
import com.ruiaa.meeting.model.vo.UserVo;
import com.ruiaa.meeting.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/reservation/")
@Validated
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    //获取所有预约信息
    @GetMapping("reservations")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<List<Reservation>> getAllReservation(){
        String username=(String) StpUtil.getLoginId();
        User user=(User)StpUtil.getSession().get(username);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return reservationService.getAllReservation();
    }

    //按需获取预约信息
    @GetMapping("reservation")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<List<Reservation>> getReservation(
           @RequestBody @Valid ReservationDto reservationDto
    )
    {
        String username=(String) StpUtil.getLoginId();
        User user=(User)StpUtil.getSession().get(username);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return reservationService.getReservation(reservationDto);
    }

    //通过预定会议
    @PostMapping("reservation")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<ReservationVo> passReservation(
         @RequestParam @NotNull (message = "预约号id不能为空")Integer reserveId
    ){
        String username=(String) StpUtil.getLoginId();
        User user=(User)StpUtil.getSession().get(username);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return reservationService.passReservation(reserveId);
    }

    //拒绝指定会议
    @PutMapping("reservation")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<ReservationVo> rejectReservation(
            @RequestParam @NotNull (message = "预约号id不能为空")Integer reserveId
    ){
        String username=(String) StpUtil.getLoginId();
        User user=(User)StpUtil.getSession().get(username);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return reservationService.rejectReservation(reserveId);
    }

    //清空过期会议
    @DeleteMapping("reservations")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<Object> clearoverdueReservation(
    ){
        String username=(String) StpUtil.getLoginId();
        User user=(User)StpUtil.getSession().get(username);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return reservationService.clearReservation();
    }

    //预约会议
    @PostMapping("appointment")
    @SaCheckRole(Constant.Role.USER)
    public CommonResponse<Object> reserveReservation(
            @RequestParam @NotNull (message = "会议室id不能为空")Integer roomId,
            @RequestParam @NotNull (message = "开始时间不能为空")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime startTime,
            @RequestParam @NotNull (message = "结束时间不能为空")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime endTime
            ){
        String username=(String) StpUtil.getLoginId();
        User user=(User)StpUtil.getSession().get(username);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return reservationService.reserveReservation(roomId,startTime,endTime);
    }

    //取消预约会议  判断username是否相同
    @DeleteMapping("appointment")
    @SaCheckRole(Constant.Role.USER)
    public CommonResponse<Object> cancelReservation(
            @RequestParam @NotNull (message = "预约id不能为空")Integer reserveId
    ){
        String username=(String) StpUtil.getLoginId();
        User user=(User)StpUtil.getSession().get(username);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }

        return reservationService.cancelReservation(reserveId);
    }
}
